/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.newnode.model;

import java.net.URL;
import java.util.ResourceBundle;
import offset.nodes.Constants;
import offset.nodes.client.dialog.newnode.model.AbstractNewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.AbstractNewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.NewHtmlFile;
import offset.nodes.client.dialog.newnode.model.NewHtmlFile;
import offset.nodes.client.dialog.newnode.model.NewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.ServiceProcessor;
import offset.nodes.client.dialog.newnode.model.Step;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.newnode.view.HtmlPanel;
import offset.nodes.client.model.HttpUtils;

/**
 *
 * @author Walter Lütgenau
 */
public class HtmlNewNodeConfigurator extends AbstractNewNodeConfigurator implements NewNodeConfigurator {
    ResourceBundle htmlBundle = ResourceBundle.getBundle("offset.nodes.client.dialog.view.DialogBundle");    

    @Override
    public ObjectType getObjectType() {
            return new ObjectType(htmlBundle, "html.name", "html.description", new Step[]{new Step(htmlBundle, "html.step1.name", new HtmlPanel())}, new ServiceProcessor(getService(), getPath()) {

                public URL process() {
                    NewHtmlFile.Request request = new NewHtmlFile.Request();
                    HtmlPanel panel = (HtmlPanel) getObjectType().getSteps()[1].getPanel();

                    request.setPath(getPath());
                    request.setName(panel.getPageName());

                    URL result = null;

                    try {
                        ServerModel model = new ServerModel(getService());
                        model.sendRequest(request);

                        if (panel.doEdit()) {
                            String resultUrl = getRepository().toString() + request.getPath() + Constants.PATH_SEPARATOR + request.getName();
                            resultUrl = HttpUtils.addParameter(resultUrl, Constants.PARVALUE_EDIT, Boolean.TRUE.toString().toLowerCase());
                            result = new URL(resultUrl);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return result;
                }
            });

    }
    
    

}
